#!/usr/bin/env groovy

def call(Map config = [:]) {

//    def podTemplate = utilities.pod_template()
    pipeline {
        agent {
            label "jenkins-maven"
        }
        environment {
            //ORG为整体项目名称，例示：klx为产品项目
            //ORG               = "${config.project.code}"
            PROJECT_NAME = "fieyijie-mro"
            ORG               = "klx"
            //APP_NAME为该项目的的名称，也为镜像的名称
            //APP_NAME          = "${config.project.appName}"
            CHARTMUSEUM_CREDS = credentials('chartmuseum')
            PREVIEW_NAMESPACE = "preview"
            TEST_NAMESPACE = "test"
            DEV_NAMESPACE = "dev"
            BUG_NAMESPACE = "bug"
            DEMO_NAMESPACE = "demo"
            buildVersion = " "
            spaces = ' '
            test404 = ' '
            PREVIEW_VERSION = "0.0.0-SNAPSHOT-$BRANCH_NAME"
        }
        stages {
            stage('获取相关代码'){
                environment {
                    projectpom = readMavenPom file: 'pom.xml'
                    projectArtifactId = projectpom.getArtifactId()
                    Artifact_ID  = "${projectArtifactId}"
                    K8S_persistence_enabled  = "false"
                    K8S_env_appParams = "--server.port=80 --spring.profiles.active=dev"
                    K8S_env_javaParams = "-Xmx512m"
                }
                steps{
                    container('maven') {
                        checkFromGit('./k8sconfig','git@gitee.com:klxedu/k8sconfig-java.git','master')
                        dir ('./k8sconfig') {
                            script{
                                K8S_health_path="actuator/health"
                                sh "mv charts/ARTIFACT_ID charts/${projectArtifactId}"
                                sh "export K8S_health_path='${K8S_health_path}' && \
                                    make replace"
                            }
                        }
                    }
                }
            }
            stage('Build && test') {
                steps {
                    container('maven') {
                        script {
                            try {
                                sh "mvn versions:set -DnewVersion=$PREVIEW_VERSION"
                                sh "mvn clean package -Dmaven.test.skip=false  -U org.sonarsource.scanner.maven:sonar-maven-plugin:3.7.0.1746:sonar -Dsonar -Dsonar.host.url=http://sonarqube-sonarqube.sonarqube:9000 -Dsonar.dynamicAnalysis=true -Dsonar.login=3ea80f90eae30c6a01a1aea736ce21b4930c2a8d -Dsonar.projectName=${PROJECT_NAME}-${BRANCH_NAME} -Dsonar.projectKey=${PROJECT_NAME}-${BRANCH_NAME}"
                            } catch (Exception e) {
                                error('Stopping early…')
                                e.printStackTrace()
                            }
                        }
                    }
                }
            }
            stage('Deploy to dev ENV') {
                when {
                    branch 'master-*'
                }
                steps {
                    script {
                        container('maven') {
                            try {
                                //Switch to App Account
                                PREVIEW_VERSION = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%H' | cut -c 1-7").trim()
                                sh "cp webapp/target/*.jar k8sconfig/dockerfile && rm -fr k8sconfig/dockerfile/*-javadoc.jar && rm -fr k8sconfig/dockerfile/*-sources.jar"
                                dir ('./k8sconfig') {
                                    env.VERSION = PREVIEW_VERSION
                                    sh "skaffold build -f skaffold.yaml"
                                }
                            } catch (Exception e) {
                                error('Stopping early…')
                                e.printStackTrace()
                            }
                        }
                    }
                }
            }
        }
        post {
            always {
                input """Pipeline debug.
                    Select Proceed or Abort to terminate the build pod"""
                cleanWs()
            }
        }
    }
}

def checkFromGit(dir,url,branches){
    checkout([$class: 'GitSCM', branches: [[name: branches]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: dir]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitee-admin', url: url]]])
}